import datetime
import json

from flask import Blueprint, jsonify
from utils.db import db1
from utils.comm import get_data
from utils.redisconn import r

bp_message = Blueprint('message',__name__)



@bp_message.route('/Announcement',methods=["POST"])
def Announcement():
    data = get_data()
    sql = "insert into notice(title,content,status,type,stime,ctime,level,notice_type) values('%s','%s',%d,%d,'%s','%s','%s','%s')"%(data["title"],data["content"],1,int(data["type"]),data["stime"],data["ctime"],data["level"],data["notice_type"])
    db1.update(sql)
    qsql = "select * from notice where title = '%s' and etime ='%s'"%(data["title"],data["ctime"])
    qres = db1.find(qsql)
    if qres:
        if qres["type"]==1:
            nowtime = datetime.datetime.strftime(datetime.datetime.now(),"%Y-%m-%d %H:%M:%S")
            for i in json.loads(data["idlist"]):
                sql1 = "insert into user_message(user_id,notice_id,status,addtime) values(%d,%d,%d,'%s')"%(int(i),int(qres["id"]),0,nowtime)
                db1.update(sql1)
        if qres["type"] == 2:
            pass
        if qres["type"] == 3:
            key = "allusernotice"+qres["id"]
            r.hset_key(key,qres["id"],qres["title"])
    return jsonify({"code":200,"msg":"添加成功"})



